package com.qlslylq.ad_x_sdk.plugin;

import android.app.Activity;
import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;

import com.qlslylq.ad_x_sdk.comm.CommRsp;
import com.qlslylq.ad_x_sdk.factory.BannerAdViewFactory;
import com.qlslylq.ad_x_sdk.factory.SplashAdViewFactory;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodChannel;

/**
 * created by qlslylq <br/>
 * on 2022/5/23 <br/>
 */
public abstract class BasePlugin implements FlutterPlugin, ActivityAware, MethodChannel.MethodCallHandler {

    private static EventChannel.EventSink eventSink;

    private FlutterPluginBinding flutterPluginBinding;

    protected MethodChannel channel;

    protected Application app;

    protected static Activity context;

    protected void onAttachedToEngine(Application app, BinaryMessenger messenger) {
        this.app = app;
        regist(messenger);
    }

    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
        flutterPluginBinding = binding;
        app = (Application) binding.getApplicationContext();
        regist(binding.getBinaryMessenger());
    }

    private void regist(BinaryMessenger messenger) {
        channel = new MethodChannel(messenger, "ad_x_sdk");
        channel.setMethodCallHandler(this);

        if (BasePlugin.eventSink == null) {
            String EVENT_CHANNEL_NAME_AD = "com.qlslylq.ad-x-sdk/adListener";
            EventChannel eventChannel = new EventChannel(messenger, EVENT_CHANNEL_NAME_AD);
            eventChannel.setStreamHandler(new EventChannel.StreamHandler() {
                public void onListen(Object o, EventChannel.EventSink eventSink) {
                    BasePlugin.eventSink = eventSink;
                }

                public void onCancel(Object o) {

                }
            });
        }
    }

    public void onDetachedFromEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        channel.setMethodCallHandler(null);
    }

    public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
        context = binding.getActivity();
        if (flutterPluginBinding != null) {
            BinaryMessenger messenger = flutterPluginBinding.getBinaryMessenger();
            flutterPluginBinding
                    .getPlatformViewRegistry()
                    .registerViewFactory(
                            "com.qlslylq.ad-x-sdk/splashAdView", new SplashAdViewFactory(binding.getActivity(), messenger));
            flutterPluginBinding
                    .getPlatformViewRegistry()
                    .registerViewFactory(
                            "com.qlslylq.ad-x-sdk/bannerAdView", new BannerAdViewFactory(binding.getActivity(), messenger));
        }
    }

    public void onDetachedFromActivityForConfigChanges() {

    }

    public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

    }

    public void onDetachedFromActivity() {

    }

    public static void sendSuccessEvent(CommRsp rsp) {
        if (eventSink != null) {
            BasePlugin.eventSink.success(rsp);
        }
    }
}
